package cn.com.sina.rd.dsp.tools;


import cn.com.sina.rd.dsp.model.Cluster;
import cn.com.sina.rd.dsp.model.Load;
import com.google.common.collect.Lists;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.Comparator;
import java.util.List;
import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * User: xinrong3
 * Date: 14-1-14
 * Time: 上午9:57
 */
public class ShowArray {
    public String [] serverArr;
    public int [][] valueArray;

    //按照JSON中array的大小降序排列。
    private static Comparator<JSONObject> comparator=new Comparator<JSONObject>() {
        @Override
        public int compare(JSONObject o1, JSONObject o2) {
            JSONArray array1=o1.getJSONArray(o1.names().getString(0));
            JSONArray array2=o2.getJSONArray(o2.names().getString(0));
            return array2.size()-array1.size();
        }
    };

    public ShowArray(JSONObject object){
        init(object);
    }

    private void init(JSONObject object) {
//        TreeSet<JSONObject> set= Sets.newTreeSet(comparator);
        List<JSONObject> set= Lists.newArrayList();
        JSONArray servers=object.getJSONArray(Cluster.SERVERS);
        Stack<JSONObject> stack=new Stack<JSONObject>();
        for(int i=0;i<servers.size();i++){
            stack.push(servers.getJSONObject(i));
        }

        while(!stack.isEmpty()){
            set.add(stack.pop());
        }

        serverArr=new String[set.size()];
        int pos=0;
        int langestLen=0;
        for(JSONObject o:set){
            serverArr[pos]=o.names().getString(0);
            JSONArray valArr=o.getJSONArray(serverArr[pos]);
            if(valArr.size()>langestLen){
                langestLen=valArr.size();
            }
            pos++;
        }

        valueArray=new int[serverArr.length][langestLen];
        pos=0;
        for(JSONObject o:set){
            JSONArray valArr=o.getJSONArray(serverArr[pos]);
            for(int i=0;i<langestLen;i++){
                if(i<valArr.size()){
                    String weight=valArr.getJSONObject(i).getString(Load.WEIGHT);
                    valueArray[pos][i]=Integer.parseInt(weight.trim());
                }else{
                    valueArray[pos][i]=0;
                }
            }
            pos++;
        }
    }

    public JSONArray getServerArray(){
        JSONArray array=new JSONArray();
        for(String key:serverArr){
            array.add(key);
        }
        return array;
    }


    public JSONArray getValueArray(){
        int firstLen=valueArray.length;
        int secLen=valueArray[0].length;
//        System.out.println(firstLen+" "+secLen);

        JSONArray array=new JSONArray();
        for(int i=0;i<secLen;i++){
            JSONObject object=new JSONObject();
            object.put("name","load_"+i);
            JSONArray data=new JSONArray();
            for(int j=0;j<firstLen;j++){
                data.add(valueArray[j][i]);
            }
            object.put("data",data);
            array.add(object);
        }
        return array;
    }
    public static void main(String...args){
//        System.out.println(ss.replaceAll("\\s+",","));
        JSONObject o=Utils.getJson();
        ShowMove showMove=new ShowMove(o);
        while(showMove.hasNext()){
            ShowArray sa=new ShowArray(showMove.next());
            System.out.println(sa.getServerArray());
            System.out.println(sa.getValueArray());
        }
    }
}
